'''MisacaAcfunResolver

Author: Cauly Kan

Date: 2001.10.19

wiki: /wiki/MisacaConverterDesign.wiki

contact: cauliflower.kan@gmail.com

'''

import urllib2
import gzip
import StringIO
import sys
import re
import Resolvers

from HTMLParser import HTMLParser

def VideoSourceResolve(ac, part = 1):
    
    ret = None
    
    m = re.match("^ac([0-9]+)$", ac)
    
    if m is not None:
        
        ac = int(m.group(1))
    
        html = ""
    
        req = urllib2.Request("http://www.acfun.tv/v/ac" + str(ac) + "/index" + (("_" + str(part)) if part > 1 else "") + ".html")
        
        req.add_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
        req.add_header("Accept-Encoding", "gzip")
        req.add_header("Accept-Charset", "utf-8")
        
        reader = urllib2.urlopen(req)
        
        data = reader.read()
        
        data = StringIO.StringIO(data)
        
        html = gzip.GzipFile(fileobj = data).read()
        
        parser = _AcfunHTMLParser()
        
        parser.feed(html)
        
        ret = parser.result
        
    else:
        
        ret = _resolve(ac)
        
    return ret
        
def _resolve(target):
    
    ret = None
    
    mods = [itm for itm in sys.modules]
    
    for mod in mods:

        if mod in Resolvers.plugins and mod != "Resolvers.MisacaAcfunResolver":
            
            if "VideoSourceResolve" in dir(sys.modules[mod]):
                
                ret = ret or sys.modules[mod].VideoSourceResolve(target)
    
    return ret

class _AcfunHTMLParser(HTMLParser):
    
    result = None
    
    embed_found = False

    def handle_starttag(self, tag, attrs):
        
        if tag == "embed":
        
            for attr in attrs:
            
                if attr[0] == "src" and attr[1] == "/newflvplayer/playert.swf":
        
                    self.embed_found = True
        
                    for itm in attrs:
                        
                        if itm[0] == "flashvars":
                            
                            print "{:>20}".format("MisacaDetect") + ": Found flashvars '%s'" % itm[1]
                            
                            vars = itm[1]
                            
                            m = re.match("^type=video&id=(.*)$", vars)
                            
                            if m is not None:
        
                                vid = "vid=" + m.group(1)
                            
                                self.result = _resolve(vid)
                                
                            m = re.match("^type2=youku&id=(.*)$", vars)
                            
                            if m is not None:
        
                                ykid = "ykid=" + m.group(1)
                            
                                self.result = _resolve(ykid)
                            
                            if self.result is None:
                            
                                print "{:>20}".format("MisacaDetect") + ": Resolve failed for '%s'" % itm[1]
                                print "{:>20}".format("MisacaDetect") + ": Probably reasons: the corresponding video source resolver is not present, video deleted by video source provider"
                            
                            return
        
        #if tag == "embed" and attrs["src"] == "http://static.loli.my/play.swf":
            
            #print attrs["flashvars"]